﻿using System.Collections.Generic;
using System.IO;
using WGS84;

namespace CIAWorldMap
{
    public static class PlacesLoader
    {
        public static void Load(Dictionary<string, Place> places)
        {
            ReadPopulation(places);
            ReadGazeteer(places);
        }

        private static void ReadGazeteer(Dictionary<string, Place> places)
        {
            const string fileLaoGazeeter = @"C:\laomapdata\la.txt";

            using (TextReader tr = new StreamReader(fileLaoGazeeter))
            {
                string line;

                while (true)
                {
                    line = tr.ReadLine();
                    if (line == null)
                        break;
                    string[] fields = line.Split('\t');

                    if (fields[3] == "LAT")
                        continue;

                    string coordKey = fields[3] + fields[4];

                    Place place;
                    if (!places.TryGetValue(coordKey, out place))
                    {
                        place = new Place(new Wgs84Coordinate((double.Parse(fields[3])), double.Parse(fields[4])));
                        places.Add(coordKey, place);
                    }

                    AddTextFieldNoComma(fields[23], place);
                    AddTextFieldNoComma(fields[26], place);
                }
                tr.Close();
            }
        }

        private static void AddTextFieldNoComma(string field, Place place)
        {
            if (field.Contains(","))
                return;

            int indexOpen = field.IndexOf('(');
            int indexClose = field.IndexOf(')');

            if ((indexOpen != -1) && (indexClose != -1) && (indexOpen < indexClose) && (indexClose - indexOpen) == 2)
            {
                field = field.Substring(0, indexOpen);
            }

            place.Add(field.Trim());
        }

        private static void ReadPopulation(Dictionary<string, Place> places)
        {
            const string f = @"C:\laomapdata\worldcitiespop.txt";

            using (TextReader tr = new StreamReader(f))
            {
                string line;

                while (true)
                {
                    line = tr.ReadLine();
                    if (line == null)
                        break;
                    string[] fields = line.Split(',');

                    if (fields[5] == "Latitude")
                        continue;

                    if (fields[0] != "la")
                        continue;

                    string coordKey = fields[5] + fields[6];

                    Place place;
                    if (!places.TryGetValue(coordKey, out place))
                    {
                        place = new Place(new Wgs84Coordinate(double.Parse(fields[5]), double.Parse(fields[6])));
                        places.Add(coordKey, place);
                    }

                    place.Add(fields[2]);
                    place.AddPopulation(fields[4]);
                }
                tr.Close();
            }
        }
    }
}
